
package uestc.bitman.edxapp.player;

import java.util.ArrayList;
import java.util.List;

public class XmlStringVideoParser implements IXmlToVideoDescriptors {

    private String xml = null;

    public XmlStringVideoParser(String xml) {
        this.xml = xml;
    }

    @Override
    public List<VideoDesciptor> parseToVideos() {

        List<VideoDesciptor> videoList = new ArrayList<VideoDesciptor>();
        int i = 0, li = 0;
        String url = "";
        do {
            i = xml.indexOf("data-mp4-source", i);
            if (i == -1) {
                break;
            }
            int ti1, ti2;
            String tempString = xml.substring(li, i);
            ti1 = tempString.lastIndexOf("</h2>");
            String tempString2 = xml.substring(li, ti1 + li);
            ti2 = tempString2.lastIndexOf("<h2>");
            String title = tempString2.substring(ti2 + 4, ti1);
            //Log.d("title", title);
            i = xml.indexOf('\"', i);
            url = xml.substring(i + 1, xml.indexOf('\"', i + 1));
            videoList.add(new VideoDesciptor(url, title));
            li = i;
        } while (!url.equals(""));

        return videoList;
    }
}
